import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * mybatis plus 代码生成器
 *
 * @author violetfreesia
 * @since 2022-02-11
 */
public class CodeGenerator {
    public static void main(String[] args) {

        ArrayList<Config> configs = new ArrayList<>() {{
            add(new Config() {{
                module = "repository";
                databaseUrl = "jdbc:mysql://47.108.136.143:4399/xyhelp_shop_repository";
                username = "xyhelp_shop";
                password = "j9LeyQL5BKp4#XJ@";
            }});
            add(new Config() {{
                module = "order";
                databaseUrl = "jdbc:mysql://47.108.136.143:4399/xyhelp_shop_order";
                username = "xyhelp_shop";
                password = "j9LeyQL5BKp4#XJ@";
            }});
            add(new Config() {{
                module = "manage";
                databaseUrl = "jdbc:mysql://47.108.136.143:4399/xyhelp_shop_manage";
                username = "xyhelp_shop";
                password = "j9LeyQL5BKp4#XJ@";
            }});
            add(new Config() {{
                module = "mall";
                databaseUrl = "jdbc:mysql://47.108.136.143:4399/xyhelp_shop_mall";
                username = "xyhelp_shop";
                password = "j9LeyQL5BKp4#XJ@";
            }});
        }};

        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .disableOpenDir()
                .fileOverride()
                .author("violetfreesia")
                .dateType(DateType.ONLY_DATE)
                .commentDate("yyyy-MM-dd")
                .build();
        String basePackage = "/src/main/java/cn/xyhelp/shop/";
        for (Config config : configs) {
            DataSourceConfig dataSourceConfig =
                    new DataSourceConfig.Builder(config.databaseUrl,
                            config.username, config.password)
                            .build();
            String pathPrefix = "./" + config.module + "/" + config.module;
            PackageConfig packageConfig = new PackageConfig.Builder()
                    .parent("cn.xyhelp.shop." + config.module)
                    .entity("model.entity")
//                    .service("service")
//                    .serviceImpl("service.impl")
//                    .mapper("dao.mapper")
                    .pathInfo(new HashMap<>() {{
                        put(OutputFile.entity, String.join("",
                                pathPrefix, "-common", basePackage, config.module, "/model/entity"));
//                        put(OutputFile.mapper, String.join("",
//                                pathPrefix, "-dao", basePackage, config.module, "/dao/mapper"));
//                        put(OutputFile.mapperXml, String.join("",
//                                pathPrefix, "-dao", "/src/main/resources/cn/xyhelp/shop/", config.module, "/dao/mapper"));
//                        put(OutputFile.service, String.join("",
//                                pathPrefix, "-service", basePackage, config.module, "/service"));
//                        put(OutputFile.serviceImpl, String.join("",
//                                pathPrefix, "-service-impl", basePackage, config.module, "/service/impl"));
                    }})
                    .build();

            StrategyConfig strategyConfig = new StrategyConfig.Builder()
                    .entityBuilder()
                    .superClass("cn.xyhelp.shop." + config.module + ".model.entity.BaseEntity")
                    .enableLombok()
                    .enableTableFieldAnnotation()
                    .enableChainModel()
                    .addIgnoreColumns("create_time", "update_time", "deleted")
//                    .serviceBuilder()
//                    .superServiceClass("cn.xyhelp.shop." + config.module + ".service.BaseService")
//                    .superServiceImplClass("cn.xyhelp.shop." + config.module + ".service.impl.BaseServiceImpl")
//                    .formatServiceFileName("%sService")
//                    .formatServiceImplFileName("%sServiceImpl")
                    .build();

            new AutoGenerator(dataSourceConfig)
                    .global(globalConfig)
                    .packageInfo(packageConfig)
                    .strategy(strategyConfig)
                    .execute();
        }

    }

    static class Config {
        String module;
        String databaseUrl;
        String username;
        String password;
    }
}
